package DBLayer;
import ModelLayer.*;
import java.sql.*;
import java.util.ArrayList;

/**
 * DBInvoice.java
 * @author Brian Lassen
 * @version 23. Marts 2012
 */
public class DBInvoice implements IFDBInvoice{

private  Connection con;
    
    public DBInvoice() 
    {
      con = DBConnection.getInstance().getDBcon();
    }

    public ArrayList<Invoice> getAllInvoices(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }
	
	@Override
	public Invoice findInvoice(int invoiceno, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Invoice searchInvoiceDate(String invoicedate,
			boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Invoice searchInvoiceAmount(int amount, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int createInvoice(Invoice myInvoice) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateInvoice(Invoice myInvoice) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteInvoice(int id) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	private ArrayList<Invoice> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<Invoice> myInvoiceList = new ArrayList<Invoice>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
			while( results.next() )
			{
				Invoice invoiceObj = new Invoice();
				invoiceObj = buildInvoice(results);	
				myInvoiceList.add(invoiceObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return myInvoiceList;
	}
	
	private Invoice singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Invoice invoiceObj = new Invoice();
	    String query =  buildQuery(wClause);
        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			invoiceObj = buildInvoice(results);
	 			stmt.close();
			}
		
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return invoiceObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Customer";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}

	private Invoice buildInvoice(ResultSet results)
      {   
		Invoice invoiceObj = new Invoice();
          try
          {
        	  invoiceObj.setInvoiceno(results.getInt(1));
        	  invoiceObj.setAmount(results.getInt(2));
        	  invoiceObj.setPaymentdate(results.getString(3));
          }
         catch(Exception e)
         {
             System.out.println("error in building the Customer object");
         }
         return invoiceObj;
      }

}
